package routes

import (
	api "gin-study/app/http/controllers"
	"gin-study/app/http/middlewares"
	"gin-study/app/http/responses"
	"gin-study/config"
	"gin-study/pkg/helper"
	"github.com/gin-gonic/gin"
)

func Setup() *gin.Engine {
	mode := config.App.Env
	if config.App.Env == "production" {
		mode = gin.ReleaseMode
	}
	if config.App.Env == "local" {
		mode = gin.DebugMode
	}
	if config.App.Env == "test" {
		mode = gin.DebugMode
	}
	gin.SetMode(mode)

	router := gin.New()

	router.Static("public", "./public")
	router.StaticFile("favicon.ico", "./public/favicon.ico")

	router.Use(
		//middlewares.Trace(),
		middlewares.Cors(),
		//middlewares.RequestLog(),
		//middlewares.Ratelimit(1, 5),
	)

	router.GET("/", helper.WarpHandle(func(c *gin.Context) interface{} {
		return responses.Ok(config.App.Name, nil)
	}))
	router.NoRoute(helper.WarpHandle(func(c *gin.Context) interface{} {
		return responses.NotFind("资源不存在", nil)
	}))
	router.NoMethod(helper.WarpHandle(func(c *gin.Context) interface{} {
		return responses.MethodNotAllowed("请求方法不被允许", nil)
	}))

	RegisterV1Routes(router)

	return router
}

func restRoute(group *gin.RouterGroup, pattern string, c api.RestController) {
	group.GET(pattern, helper.WarpHandle(c.Index))
	group.POST(pattern, helper.WarpHandle(c.Post))
	group.GET(pattern+"/:id", helper.WarpHandle(c.Get))
	group.PUT(pattern+"/:id", helper.WarpHandle(c.Put))
	group.DELETE(pattern+"/:id", helper.WarpHandle(c.Delete))
}
